package database;

import database.employee.tb_employee;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class GUIemployee {
    JFrame frame;
    private JMenuBar menuBar;
    private JMenu fileMenu, infoMenu,AccountMenu;
    private JMenuItem employeeItem, productItem, customerItem;
    private JMenuItem purchaseMainItem, purchaseDetailItem;
    private JMenuItem AccountItem;
    public GUIemployee(tb_employee e1) {
        frame = new JFrame("超市管理系统");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(450, 300);
        frame.setLocationRelativeTo(null); // 居中显示

    // 创建菜单栏
    menuBar = new JMenuBar();

    // 创建顶层菜单
    fileMenu = new JMenu("采购信息");
    infoMenu = new JMenu("基本信息");
    AccountMenu =new JMenu("账号信息");
    // 创建菜单项
    employeeItem = new JMenuItem("员工信息");
    productItem = new JMenuItem("商品信息");
    customerItem = new JMenuItem("客户信息");
    purchaseMainItem = new JMenuItem("采购主表");
    purchaseDetailItem = new JMenuItem("采购详细表");
    AccountItem=new JMenuItem("账号信息");
    //给菜单键添加监听
        employeeItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showDatabaseTable("tb_employee");
        }
    });
        productItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showDatabaseTable("tb_good");
        }
    });
        customerItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showDatabaseTable("tb_customer");
        }
    });
        purchaseMainItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showDatabaseTable("tb_pay_main");
        }
    });
        purchaseDetailItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showDatabaseTable("tb_pay_detail");
        }
    });
        AccountItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new EmployeeInfoPanel(e1);
            }
        });
        AccountMenu.add(AccountItem);
    // 将菜单项添加到信息菜单中
        infoMenu.add(employeeItem);
        infoMenu.add(productItem);
        infoMenu.add(customerItem);

    // 将采购相关的菜单项添加到文件菜单中
        fileMenu.add(purchaseMainItem);
        fileMenu.add(purchaseDetailItem);

    // 将菜单添加到菜单栏
        menuBar.add(fileMenu);
        menuBar.add(infoMenu);
        menuBar.add(AccountMenu);
    // 将菜单栏添加到框架
        frame.setJMenuBar(menuBar);

    // 设置框架可见
        frame.setVisible(true);
}
    public void showDatabaseTable(String tableName) {
        new Thread(() -> {
            // 数据库连接信息
            String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
            String user = "root";
            String password = "123456";

            // 直接在查询中使用表名，不使用参数化查询
            String query = "SELECT * FROM " + tableName;

            try {
                Connection conn = DriverManager.getConnection(url, user, password);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(query);

                // 从ResultSet获取列名
                ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                String[] columnNames = new String[columnCount];
                for (int i = 0; i < columnCount; i++) {
                    columnNames[i] = metaData.getColumnName(i + 1); // 列索引从1开始
                }

                // 创建数据模型并填充数据
                DefaultTableModel model = new DefaultTableModel(columnNames, 0);
                while (rs.next()) {
                    Object[] row = new Object[columnCount];
                    for (int i = 0; i < columnCount; i++) {
                        row[i] = rs.getObject(i + 1); // 行数据索引从1开始
                    }
                    model.addRow(row);
                }

                // 假定您已经有了一个JTable和对应的模型model
                JTable table = new JTable(model);

                // 创建用于显示JTable的JFrame
                JFrame tableFrame = new JFrame(tableName);
                tableFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                tableFrame.add(new JScrollPane(table), BorderLayout.CENTER); // 将JTable添加到中心区域

                // 创建面板用于容纳按钮，并设置布局管理器
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new FlowLayout()); // 使用FlowLayout作为示例，您也可以使用其他布局管理器


                // 将按钮面板添加到JFrame的南部
                tableFrame.add(buttonPanel, BorderLayout.SOUTH);

                // 根据组件preferred size设置JFrame大小
                tableFrame.pack();

                // 居中显示JFrame
                tableFrame.setLocationRelativeTo(null);

                // 设置JFrame可见
                SwingUtilities.invokeLater(() -> tableFrame.setVisible(true));

            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "数据库连接或查询失败: " + ex.getMessage());
                ex.printStackTrace();
            }
        }).start();
    }
}
